import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop/common/index.dart';
import 'package:flutter_shop/common/utils/gap.dart';
import 'package:flutter_shop/pages/add_address_page.dart';
import 'package:flutter_shop/provide/address_provider.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:provider/provider.dart';

class AddressPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _AddressPageState();
  }
}

class _AddressPageState extends State<AddressPage> with AfterLayoutMixin {
  SlidableController _slidableController;

  @override
  void initState() {
    _slidableController = SlidableController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer(
      builder: (BuildContext context, AddressProvider val, child) {
        return Scaffold(
            appBar: AppBar(
              leading: IconButton(
                icon: Icon(Icons.arrow_back_ios),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
              centerTitle: true,
              title: Text('地址'),
              actions: <Widget>[
                IconButton(
                  icon: Icon(Icons.add),
                  tooltip: '添加地址',
                  onPressed: () {
                    RouteUtil.push(context, AddAddressPage());
                  },
                )
              ],
            ),
            body: Container(
                child: val.addressList.length == 0
                    ? buildPageEmpty()
                    : ListView.builder(
                        itemCount: val.addressList.length,
                        itemBuilder: (BuildContext context, int pos) {
                          return _buildAddressItem(context, pos, val);
                        })));
      },
    );
  }

  Widget _buildAddressItem(BuildContext context, int pos, AddressProvider val) {
    var item = val.addressList[pos];
    return Slidable(
      controller: _slidableController,
      actionPane: SlidableDrawerActionPane(),
      secondaryActions: <Widget>[
        IconSlideAction(
          caption: '删除',
          color: Colors.red,
          icon: Icons.delete,
          onTap: () {
            val.remove(item);
          },
        ),
      ],
      child: Container(
        alignment: Alignment.centerLeft,
        padding: EdgeInsets.all(10),
        color: Colors.white,
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Text(
                  item.name,
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(16), color: Colors.black),
                ),
                10.hGap,
                Text(
                  item.phone,
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(14), color: Colors.black26),
                ),
              ],
            ),
            10.vGap,
            Container(
              alignment: Alignment.centerLeft,
              child: Text(
                item.address,
                textAlign: TextAlign.start,
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(16), color: Colors.black26),
              ),
            ),
            10.vGap,
//        Row(
//          children: <Widget>[
//            Checkbox(
//              value: item.isDefault,
//              onChanged: (bool value) {
//                item.isDefault = value;
//                val.change(item);
//              },
//            ),
//            Text('默认地址'),
//            Expanded(child: SizedBox()),
//            MaterialButton(
//              child: Text('编辑'),
//              onPressed: () {
//                TopReminder.error(context, '编辑开发中');
//              },
//            ),
//          ],
//        ),
          ],
        ),
      ),
    );
  }

  @override
  void afterFirstLayout(BuildContext context) {
    context.read<AddressProvider>().refresh();
  }
}
